package com.shop.loan.proxy;

import com.alibaba.fastjson.JSON;
import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
import com.shop.loan.mapper.LoanOrderPayLogByMybatisMapper;
import com.shop.loan.model.entity.LoanOrderBillByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.entity.LoanOrderPayLogByMybatis;
import com.shop.loan.model.view.StatisticsByOverduePay;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsByOverduePayProxy implements DataProxy<StatisticsByOverduePay> {

    @Resource
    Util util;

    @Resource
    LoanOrderPayLogByMybatisMapper loanOrderPayLogByMybatisMapper;

    @Resource
    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;

    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.clear();
        return null;
    }


    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        String channelIdStr = util.getSearchParam("channel", 1);
        String authServiceUserStr = util.getSearchParam("authServiceUser", 1);
        String loanServiceUserStr = util.getSearchParam("loanServiceUser", 1);
        String afterServiceUserStr = util.getSearchParam("afterServiceUser", 1);
        String collectionServiceUserStr = util.getSearchParam("collectionServiceUser", 1);
//        String expireStatusStr = util.getSearchParam("expireStatus", 1);
        String payTimeStartStr = util.getSearchParam("payTime", 0);
        String payTimeEndStr = util.getSearchParam("payTime", 1);
        LoanOrderByMybatis search = new LoanOrderByMybatis();
        LoanOrderPayLogByMybatis loanOrderPayLogByMybatis = new LoanOrderPayLogByMybatis();
        search.setAuthStatus(1);
        if (StringUtils.hasLength(channelIdStr)) {
            search.setChannelId(Long.parseLong(channelIdStr));
            loanOrderPayLogByMybatis.setChannelId(Long.parseLong(channelIdStr));
        }
        if (StringUtils.hasLength(payTimeStartStr)) {
            search.setStartTime(payTimeStartStr);
            loanOrderPayLogByMybatis.setStartTimeByPay(payTimeStartStr);
        }

        if (StringUtils.hasLength(payTimeEndStr)) {
            search.setEndTime(payTimeEndStr);
            loanOrderPayLogByMybatis.setEndTimeByPay(payTimeEndStr);
        }

        if (StringUtils.hasLength(authServiceUserStr)) {
            search.setAuthServiceId(Long.parseLong(authServiceUserStr));
            loanOrderPayLogByMybatis.setAuthServiceId(Long.parseLong(authServiceUserStr));
        }
        if (StringUtils.hasLength(loanServiceUserStr)) {
            search.setLoanServiceId(Long.parseLong(loanServiceUserStr));
            loanOrderPayLogByMybatis.setLoanServiceId(Long.parseLong(loanServiceUserStr));
        }
        if (StringUtils.hasLength(afterServiceUserStr)) {
            search.setAfterServiceId(Long.parseLong(afterServiceUserStr));
            loanOrderPayLogByMybatis.setAfterServiceId(Long.parseLong(afterServiceUserStr));
        }
        if (StringUtils.hasLength(collectionServiceUserStr)) {
            search.setCollectionServiceId(Long.parseLong(collectionServiceUserStr));
            loanOrderPayLogByMybatis.setCollectionServiceId(Long.parseLong(collectionServiceUserStr));
        }
        Integer orderNum = loanOrderBillByMybatisMapper.selectCount(search);
        orderNum = orderNum == null ? 0 : orderNum;
        if (!StringUtils.hasLength(payTimeStartStr) && !StringUtils.hasLength(payTimeEndStr)) {
            orderNum = orderNum / 2;
        }
        search.setPayStatus(1);
        Integer payOrderNum = loanOrderBillByMybatisMapper.selectCount(search);
        payOrderNum = payOrderNum == null ? 0 : payOrderNum;
        if (!StringUtils.hasLength(payTimeStartStr) && !StringUtils.hasLength(payTimeEndStr)) {
            payOrderNum = payOrderNum / 2;
        }

        search.setPayStatus(null);
        LoanOrderBillByMybatis sumInfo = loanOrderBillByMybatisMapper.selectSum(search);
        if (sumInfo == null) {
            sumInfo = new LoanOrderBillByMybatis();
            sumInfo.setMoney(BigDecimal.ZERO);
            sumInfo.setRateMoney(BigDecimal.ZERO);
        }
        BigDecimal money = sumInfo.getMoney().add(sumInfo.getRateMoney());
        //M0还款金额
        loanOrderPayLogByMybatis.setSearchType(1);
        BigDecimal moneyByM0 = loanOrderPayLogByMybatisMapper.selectSumByOverdue(loanOrderPayLogByMybatis);
        moneyByM0 = moneyByM0 == null ? BigDecimal.ZERO : moneyByM0;
        BigDecimal moneyRateByM0 = moneyByM0.divide(money.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ONE : money, 2, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(100));

        //M1还款金额
        loanOrderPayLogByMybatis.setSearchType(2);
        loanOrderPayLogByMybatis.setOverdueDayStart(0);
        loanOrderPayLogByMybatis.setOverdueDayEnd(30);
        BigDecimal moneyByM1 = loanOrderPayLogByMybatisMapper.selectSumByOverdue(loanOrderPayLogByMybatis);
        moneyByM1 = moneyByM1 == null ? BigDecimal.ZERO : moneyByM1;
        BigDecimal moneyRateByM1 = moneyByM1.divide(money.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ONE : money, 2, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(100));

        StatisticsByOverduePay statisticsByOverduePay = new StatisticsByOverduePay();
        statisticsByOverduePay.setOrderNum(orderNum);
        statisticsByOverduePay.setPayOrderNum(payOrderNum);
        statisticsByOverduePay.setMoney(money);
        statisticsByOverduePay.setMoneyByM0(moneyByM0);
        statisticsByOverduePay.setMoneyRateByM0(moneyRateByM0);
        statisticsByOverduePay.setMoneyByM1(moneyByM1);
        statisticsByOverduePay.setMoneyRateByM1(moneyRateByM1);
        Map map = JSON.parseObject(JSON.toJSONString(statisticsByOverduePay), Map.class);
        list.add(map);
    }

}
